﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using ConsoleApplication.Model;

namespace ConsoleApplication.BLL
{
    public class ParameterSerialize
    {
        public static string SerializeParameters(Parameter[] Parameters)
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            XmlRootAttribute Root = new XmlRootAttribute();
            Root.ElementName = "Parameters";

            XmlSerializer sr = new XmlSerializer(typeof(Parameter[]), Root);
            sr.Serialize(sw, Parameters);

            sw.Close();

            return sb.ToString();
        }

        public static Parameter[] DeSerializeParameters(string XMLStr)
        {
            XmlRootAttribute Root = new XmlRootAttribute();
            Root.ElementName = "Parameters";

            XmlSerializer sr = new XmlSerializer(typeof(Parameter[]), Root);

            StringReader strReader = new StringReader(XMLStr);
            System.Xml.XmlReader r = new System.Xml.XmlTextReader(strReader);
            try
            {
                Parameter[] Result = (Parameter[])sr.Deserialize(r);
                return Result;
            }
            finally
            {
                strReader.Close();
                r.Close();
            }
        }


        public static string ReadFile(string FileName)
        {
            StreamReader r = new StreamReader(FileName);
            try
            {
                return r.ReadToEnd();
            }
            finally
            {
                r.Close();
            }
        }
    }
}
